package com.spirng.leetcode;

import static com.spirng.leetcode.Solution2.minSubArrayLen;

public class Solution {
    public static void main(String[] args) {
     int []num={2,3,1,2,4,3};
        int i = minSubArrayLen(7, num);
        System.out.println(i);
    }
    public int[] twoSum(int[] nums, int target) {
        int length = nums.length;
        int i,j=0;
        int number[]=new int[2];
        for (i=0;i<length;i++){
            for (j=1;j<=length-1;j++){
                if(target==nums[i]+nums[j]){
                    return number= new int[]{i, j};
                }
            }
        }
        return number;
    }
}
class Solution2 {
    public int[] sortedSquares(int[] nums) {
        int j=nums.length-1;
        int k=j;

        int [] result=new int[nums.length];
        for(int i=0;i<=j;){
            if(nums[i]*nums[i]<nums[j]*nums[j]){
                result[k--]=nums[j]*nums[j];
                j--;
            }else{
                result[k--]=nums[i]*nums[i];
                i++;
            }
        }
        return result;
    }
    public static  int minSubArrayLen(int target, int[] nums) {
        int i=0;
        int j=0;
        int sum=0;
        int result=0 ;
        int sub=0;
        for(j=0;j<nums.length;j++){
            sum+=nums[j];
            while(sum>=target){
                sub=j-i+1;
                sum=sum-nums[i++];
                result=result<sub?result:sub;
            }
        }
        return result;
    }
}
